package com.lolong.main;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.lolong.main.WeatherConnector;

public class HomeScreenView extends Activity implements OnClickListener {
	
	WeatherConnector weatherConnector;
	Button eat, about, help;
	public boolean connected;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.homescreen);
		
		eat = (Button) findViewById(R.id.homescreenButtonEat);
		eat.setOnClickListener(this);
		
		help = (Button) findViewById(R.id.homescreenButtonHelp);
		help.setOnClickListener(this);
		
		about = (Button) findViewById(R.id.homescreenButtonAbout);
		about.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
		switch(v.getId()) {
		
			case R.id.homescreenButtonEat:
					Intent pricePage = new Intent("com.lolong.main.STARTVIEW"); 
					startActivity(pricePage);
				break;
				
			case R.id.homescreenButtonHelp:
				Intent helpPage = new Intent("com.lolong.main.HELPVIEW"); 
				startActivity(helpPage);
				break;
				
			case R.id.homescreenButtonAbout:
				Intent aboutPage = new Intent("com.lolong.main.ABOUTVIEW");
				startActivity(aboutPage);
				break;
		}	
	}
	
	//Checks connection *Don't forget to add android.permission.ACCESS_NETWORK_STATE
		public boolean checkConn(Context ctx) {
	        
			ConnectivityManager conMgr =  (ConnectivityManager)ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
	        NetworkInfo i = conMgr.getActiveNetworkInfo();
	        
	        if (i == null)
	          return false;
	        if (!i.isConnected())
	          return false;
	        if (!i.isAvailable())
	          return false;
	        
	        return true;
	        
	    }
		
	@Override
	public void onBackPressed() {
		
		//Set if connected
		connected = checkConn(HomeScreenView.this);
		
		if(connected) {
	
			weatherConnector = new WeatherConnector();
			
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage(weatherConnector.returnSuggestion() + " Thanks for trusting me. ;)")
				.setCancelable(false)
				.setNegativeButton("Ok", new DialogInterface.OnClickListener() {
					
					public void onClick(DialogInterface dialog, int id) {
						HomeScreenView.this.finish();
					}
				});
			
			AlertDialog alert = builder.create();
			alert.show();
			
		} else {
			
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage("It appaers that you don't have an internet connection. Sorry, I can't provide clothign suggestion. :(")
				.setCancelable(false)
				.setNegativeButton("Ok", new DialogInterface.OnClickListener() {
					
					public void onClick(DialogInterface dialog, int id) {
						HomeScreenView.this.finish();
					}
				});
			
			AlertDialog alert = builder.create();
			alert.show();
		}
	} 
}
